package com.tucanstubs.android_wheresmystuff.activities;


import java.util.List;
import com.tucanstubs.android_wheresmystuff.R;
import com.tucanstubs.android_wheresmystuff.login.User;
import com.tucanstubs.android_wheresmystuff.database.UserHelper;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

/**
 * This activity displays information regarding a user. It also allows the
 * currently logged in admin to unlock the user, delete the user, or prompt the
 * user to an admin.
 * @author team 37
 * @version 2.0 4/9/2013
 *
 */
public class UserInfoActivity extends Activity  {
	
	private User selectedUser;
	private User loggedOnUser;
	
	/**
	 * Creates the activity. Initializes all GUI Components and retrieves information form the bundle.
	 *  The activity receives a bundle with keys Tags.USERNAME, and 
	 * Tags.SELECTEDUSER. Tags.USERNAME maps to the user name of the currently logged in user.
	 * Tags.SELECTEDUSER maps to the user name of the user whose information we are displaying
	 */
	public void onCreate(Bundle SavedInstanceState){
		super.onCreate(SavedInstanceState);
		setContentView(R.layout.user_info_screen);
		Bundle bundle=getIntent().getExtras();
		selectedUser=UserHelper.get(this,bundle.getString(Tags.SELECTEDUSER));
		
		//Fills the list with the user information
		List<String> userInfo = selectedUser.toList();
		ListView userInfoView = (ListView) findViewById(R.id.User_Info_List);
		ListAdapter userInfoAdapter = new ArrayAdapter<String>(this,
				  android.R.layout.simple_list_item_1, android.R.id.text1, userInfo);
		userInfoView.setAdapter(userInfoAdapter);
		loggedOnUser = UserHelper.get(this, bundle.getString(Tags.USERNAME));
	}

	/**
	 * Method which is called when a button is pressed. The selected user is either made an admin,
	 * unlocked, or deleted. A check is included so that user's cannot delete themselves.
	 * @param view the button which was clicked
	 */
	public void onClick(View view){
		switch(view.getId()){
		//makes the selected user into an admin
		case R.id.Make_Admin_Button:
			UserHelper.add(getApplicationContext(), selectedUser.makeAdmin());
			finish();
			startActivity(getIntent());
			break;
		//deletes the selected user
		//makes sure user is not deleting them self
		case R.id.Delete_Button:
			Toast toast;
			if (selectedUser.getUserName().equals(loggedOnUser.getUserName())) {
				
				toast = Toast.makeText(this, "You can't delete yourself", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			} else {
				
				UserHelper.remove(this,selectedUser);
				finish();
			}
			break;
		//unlocks the selected user's account
		case R.id.Unlock_Account_Button:
			selectedUser.setLockCount(0);
			selectedUser.setLocked(false);
			UserHelper.add(this,selectedUser);
			finish();
			startActivity(getIntent());
			break;
		}
	}
}

